package com.example.ordersystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ordersystem.entity.User;
import com.example.ordersystem.mapper.UserMapper;
import com.example.ordersystem.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 用户注册
     */
    @Override
    public void register(User user) {
        //设置默认头像
        user.setAvatar("https://sjt-bucket.oss-cn-hangzhou.aliyuncs.com/img/boy.png");
        userMapper.insert(user);
    }

    /**
     * 用户登录
     * @param user 用户对象
     * @return 是否登录成功
     */
    @Override
    public User login(User user) {
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<User>().
                eq("mailbox",user.getMailbox()).
                eq("pwd",user.getPwd());
        User user1 = userMapper.selectOne(userQueryWrapper);
        return user1;
    }

    /**
     * 修改密码
     * @param user 用户对象
     */
    @Override
    public void changePwd(User user) {
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<User>().
                eq("mailbox",user.getMailbox());
        userMapper.update(user,userQueryWrapper);
    }

    /**
     * 根据id查询用户个人信息
     * @param id 用户id
     * @return 用户对象
     */
    @Override
    public User getInfoById(String id) {
        return userMapper.selectById(id);
    }

    /**
     * 修改用户个人信息
     * @param user 用户对象
     */
    @Override
    public void changeInfo(User user) {
        userMapper.updateById(user);
    }

    /**
     * 修改头像
     * @param id 用户id
     * @param avatar 头像地址
     */
    @Override
    public void changeAvatar(String id, String avatar) {
        User user = new User();
        user.setAvatar(avatar);
        user.setId(Integer.parseInt(id));
        userMapper.updateById(user);
    }

    /**
     * 管理员查看用户列表
     */
    @Override
    public Page<User> selectUserList(String currentPage) {
        return userMapper.selectPage(new Page<>(Integer.parseInt(currentPage),10),null);
    }
}
